<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>App2</title>
</head>
<body>
	<h1>App2</h1>
	<div>
		您的用戶名是：<span id="user-name-label"></span>
	</div>
	<div id="response"></div>
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
	<script type="text/javascript">
		function getQueryVariable(variable) {
			var query = window.location.search.substring(1);
			var vars = query.split("&");
			for (var i = 0; i < vars.length; i++) {
				var pair = vars[i].split("=");
				if (pair[0] == variable) {
					return pair[1];
				}
			}
			return "";
		}

		function getCookie(c_name) {
			if (document.cookie.length > 0) {
				c_start = document.cookie.indexOf(c_name + "=")
				if (c_start != -1) {
					c_start = c_start + c_name.length + 1
					c_end = document.cookie.indexOf(";", c_start)
					if (c_end == -1)
						c_end = document.cookie.length
					return unescape(document.cookie.substring(c_start, c_end))
				}
			}
			return ""
		}

		function setCookie(name, value) {
			//设置名称为name,值为value的Cookie
			var expdate = new Date(); //初始化时间
			expdate.setTime(expdate.getTime() + 60 * 1000); //时间,60s
			document.cookie = name + "=" + value + ";expires="
					+ expdate.toGMTString() + ";path="
					+ window.location.pathname;
			//即document.cookie= name+"="+value+";path=/"; 时间可以不要，但路径(path)必须要填写，因为JS的默认路径是当前页，如果不填，此cookie只在当前页面生效！~
		}
	</script>
	<script type="text/javascript">
		function success_jsonpCallback(res) {
			console.log(res)
		}
		if (getQueryVariable("jwt") != "") {
			console.log(getQueryVariable("jwt"))
			$
					.post({
						url : "http://192.168.0.105:8089/hswapp2-backend/checkjwt",
						method : "GET",
						dataType : 'jsonp',
						jsonp : 'callback',//自定义
						jsonpClaaback : "success_jsonpCallback",//用户定义的callback函数，没有定义的话会jQuery会自动生成以jQuery开头的函数 

						async : false,
						data : {
							jwt : getQueryVariable("jwt")
						},
						success : function(res) {
							console.log("Query JWT Seccess")
							console.log(res)
							if (res.userInfo) {
								// Check JWT Success
								setCookie("hswapp2_jwt",
										getQueryVariable("jwt"))
								$("#user-name-label").text(res.userInfo)
							} else {
								window.location.href = "http://192.168.0.105:8089/oauth/authorize?redirect_url=http://192.168.0.105:8089/hswapp2-backend/authorize-callback&response_type=code&client_id=hsw-app2&scope=read&appsite=http://192.168.0.105/hsw-app2"
							}
						}
					})
		} else if (getCookie("hswapp2_jwt") != "") {
			console.log(getCookie("hswapp2_jwt"))
			$
					.post({
						url : "http://192.168.0.105:8089/hswapp2-backend/checkjwt",
						method : "GET",
						dataType : 'jsonp',
						jsonp : 'callback',//自定义
						jsonpClaaback : "success_jsonpCallback",//用户定义的callback函数，没有定义的话会jQuery会自动生成以jQuery开头的函数 

						async : false,
						data : {
							jwt : getCookie("hswapp2_jwt")
						},
						success : function(res) {
							console.log("Query JWT Seccess")
							console.log(res)
							if (res.userInfo) {
								$("#user-name-label").text(res.userInfo)
							} else {
								window.location.href = "http://192.168.0.105:8089/oauth/authorize?redirect_url=http://192.168.0.105:8089/hswapp2-backend/authorize-callback&response_type=code&client_id=hsw-app2&scope=read&appsite=http://192.168.0.105/hsw-app2"
							}
						}
					})
		} else {
			window.location.href = "http://192.168.0.105:8089/oauth/authorize?redirect_url=http://192.168.0.105:8089/hswapp2-backend/authorize-callback&response_type=code&client_id=hsw-app2&scope=read&appsite=http://192.168.0.105/hsw-app2"
		}
	</script>
</html>
